﻿/*****************************************************************
// Copyright (C)
// All rights reserved.
// 
// Author:       WYQ
// Create Date:  2012-8-18 15:15:57
// Usage: 持久化数据库仓库
*****************************************************************/
using System;
using System.Linq;
using System.Linq.Expressions;

namespace Portal.Ef.Repository
{

   
    public class  ActionRepository : Repository<Action, Action, TbAction>, IActionRepository<Action>
    {
        public override Action CreateEntity()
        {
            return new Action { };
        }
        public override IQueryable<Action> Get()
        {
            return MapQueryable(Entities.TbAction);
        }
        public override long Count()
        {
            return MapQueryable(Entities.TbAction).Count();
        }
        public override IQueryable<Action> Get(Expression<Func<Action, bool>> expression)
        {
            return MapQueryable(Entities.TbAction).Where(expression);
        }
        public override IQueryable<Action> GetSort(string sort = "Id", string order = "DESC")
        {
            Check.Argument.IsNotNull(sort, "排序字段");
            Check.Argument.IsNotNull(order, "排序条件");
            return LinqHelper.DataSorting(MapQueryable(Entities.TbAction), sort, order);
        }
        public override IQueryable<Action> GetSort(Expression<Func<Action, bool>> expression, string sort = "Id", string order = "DESC")
        {
            Check.Argument.IsNotNull(expression, "查询条件");
            Check.Argument.IsNotNull(sort, "排序字段");
            Check.Argument.IsNotNull(order, "排序条件");
            return LinqHelper.DataSorting(MapQueryable(Entities.TbAction).Where(expression), sort, order);
        }
        public override void Add(Action t)
        {
            Entities.AddToTbAction(ReverseMapModel(t));
        }
        public override void Update(Action t)
        {
            throw new NotImplementedException();
        }
        public override void AddOrUpdate(Action t)
        {
            if (Entities.TbAction.Any(o => o.CoActionId == t.Id))
            {

            }
            else
            {
                Entities.AddToTbAction(ReverseMapModel(t));
            }
        }
        public override void Delete(Action t)
        {
            Entities.TbAction.Where(o => o.CoActionId == t.Id).ForEach(o => Entities.DeleteObject(o));
        }
        public override void Delete(Expression<Func<Action, bool>> expression)
        {
            MapQueryable(Entities.TbAction).Where(expression).ForEach(o => Entities.DeleteObject(o));
        }
        public override void Save()
        {
            Entities.SaveChanges();
        }
       
        public JqEasyuiDataGrid<Action> JqEasyuiDataGrid(string code, string name, string categoryId, string sort = "CreateTime", string order = "Desc", int page = 10, int index = 1)
        {
            return new JqEasyuiDataGrid<Action>();
        }
      
        public override sealed IQueryable<Action> MapQueryable(IQueryable<TbAction> queryable)
        {
            return queryable.Select(o => new Action 
            {
         
	           Id=o.CoActionId,
	           Name = o.CoName 
,
	           ControllerName = o.CoControllerName 
,
	           ActionName = o.CoActionName 
,
	           Area = o.CoArea 
,
	           Type = o.CoType 
,
	           Url = o.CoUrl 
,
	           CreateTime = o.CoCreateTime??DateTime.Now 
,
	           IsDelete = o.CoIsDelete??false 
,
	           Version = o.CoVersion??0 
,
	           IsClosed = o.CoIsClosed??false 
,
	           Culture = o.CoCulture 
,
	           Sort = o.CoSort??0 

            
            });
        }
      
        public override sealed IQueryable<TbAction> ReverseMapQueryable(IQueryable<Action> queryable)
        {
            return queryable.Select(o => new TbAction 
            { 
        
	           CoActionId=o.Id,
	           CoName = o.Name
,
	           CoControllerName = o.ControllerName
,
	           CoActionName = o.ActionName
,
	           CoArea = o.Area
,
	           CoType = o.Type
,
	           CoUrl = o.Url
,
	           CoCreateTime = o.CreateTime
,
	           CoIsDelete = o.IsDelete
,
	           CoVersion = o.Version
,
	           CoIsClosed = o.IsClosed
,
	           CoCulture = o.Culture
,
	           CoSort = o.Sort

  
            
            });
        }
      
        public override sealed Action MapModel(TbAction model)
        {
            return new Action {
	           Id=model.CoActionId,
	           Name = model.CoName
,
	           ControllerName = model.CoControllerName
,
	           ActionName = model.CoActionName
,
	           Area = model.CoArea
,
	           Type = model.CoType
,
	           Url = model.CoUrl
,
	           CreateTime = model.CoCreateTime??DateTime.Now
,
	           IsDelete = model.CoIsDelete??false
,
	           Version = model.CoVersion??0
,
	           IsClosed = model.CoIsClosed??false
,
	           Culture = model.CoCulture
,
	           Sort = model.CoSort??0

            };
        }
        
        public override sealed TbAction ReverseMapModel(Action model)
        {
            return new TbAction { 
	           CoActionId=model.Id,
	           CoName = model.Name
	        
,
	           CoControllerName = model.ControllerName
	        
,
	           CoActionName = model.ActionName
	        
,
	           CoArea = model.Area
	        
,
	           CoType = model.Type
	        
,
	           CoUrl = model.Url
	        
,
	           CoCreateTime = model.CreateTime
	        
,
	           CoIsDelete = model.IsDelete
	        
,
	           CoVersion = model.Version
	        
,
	           CoIsClosed = model.IsClosed
	        
,
	           CoCulture = model.Culture
	        
,
	           CoSort = model.Sort
	        

	        
            
            
            };
        }


    }
}



